package com.sunyard.domee.common.filter;

import com.sunyard.domee.commons.utils.TraceIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

/**
 * @program domee
 * @description 可以拦截到MDC
 * @author raoinyx@163.com
 * @create 2020-07-18 00:23
 */
@Slf4j
@Component
public class LogMdcFilter implements Filter {

    public static final String MDC_TRADE_ID = "INNER_TRACE_ID";

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        try {
            MDC.put(MDC_TRADE_ID, TraceIDGenerator.generate());
            chain.doFilter(servletRequest, servletResponse);
        }finally {
            MDC.remove(MDC_TRADE_ID);
        }
    }

}
